<template>
  <div class="partner-list" v-loading.fullscreen.lock="loading">
    <page-main :pagination="pagination" @search-data="searchData" @page-change="pageChange">
      <template slot="search">
        <search-form :search="search"></search-form>
      </template>
      <template slot="append">
        <el-button type="primary" size="medium" @click="addPartner">新增</el-button>
      </template>
      <template slot="table">
        <el-table fit stripe border highlight-current-row :data="partnerList" height="100%">
          <el-table-column prop="partnerName" label="分店名称" min-width="120px" header-align="center">
          </el-table-column>
          <el-table-column prop="branchName" label="所属总店" min-width="120px" header-align="center">
          </el-table-column>
          <el-table-column prop="majorName" label="主营类型" min-width="100px" header-align="center">
          </el-table-column>
          <el-table-column prop="partnerPhone" label="联系电话" min-width="120px" header-align="center">
          </el-table-column>
          <el-table-column prop="partnerAddress" label="分店地址" min-width="130px" header-align="center">
          </el-table-column>
          <el-table-column prop="partnerWechatMchId" label="商户号" min-width="130px" header-align="center">
          </el-table-column>
          <el-table-column :resizable="false" prop="status" width="80px" label="状态" align="center">
            <template slot-scope="scope">
              <span :class="'status_'+scope.row.status">{{i18n.getMsg(`branch.status.${scope.row.status}`)}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="操作" width="80px" align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="updatePartner(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </page-main>

    <el-dialog :title="dialogType==='add'?'新增':'修改'" custom-class="sys-dialog" width="600px" :close-on-click-modal="false" :visible.sync="openDialog"
      top="10vh" @closed="dialogClosed">
      <div style="height:calc(80vh - 54px);overflow:auto">
        <partner-form v-if="openDialog" :type="dialogType" :partner="selectPartner" :dropdown-option="dropdownOption" @save-success="saveSuccess">
        </partner-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import BranchApi from 'services/branchApi';

  import PageMain from 'commonComponents/pageMain';
  import SearchForm from './searchForm';
  import PartnerForm from './partnerForm';

  import Pagination from 'models/pagination';
  import PartnerListSearch from 'models/branch/partnerListSearch';

  export default {
    data() {
      return {
        loading: true,
        pagination: new Pagination(),
        search: new PartnerListSearch(),
        partnerList: [],
        openDialog: false,
        selectPartner: null,
        dialogType: '',
        dropdownOption: {}
      }
    },

    created() {
      this.setPartnerList(true);
    },

    methods: {
      setPartnerList(isSearch = false) {
        let search = this.search;
        if (!isSearch) {
          search.resetOldData();
        }

        let pagination = this.pagination;
        this.loading = true;
        BranchApi.getAllPartner(search, pagination).then(partnerList => {
          if (isSearch) {
            search.setOldData();
          }

          if (partnerList.length === 0 && pagination.currentPage > 1) {
            this.$nextTick(() => {
              this.setPartnerList(isSearch);
            });
            return;
          }

          this.partnerList = partnerList;
          this.loading = false;
        }).catch(() => {
          this.loading = false;
        });
      },

      searchData() {
        this.pagination.currentPage = 1;
        this.setPartnerList(true);
      },

      pageChange() {
        this.setPartnerList();
      },

      addPartner() {
        this.dialogType = 'add';
        this.openDialog = true;
      },

      updatePartner(partner) {
        this.dialogType = 'update';
        this.selectPartner = partner;
        this.openDialog = true;
      },

      dialogClosed() {
        this.dialogType = '';
        this.selectPartner = null;
      },

      saveSuccess() {
        this.openDialog = false;
        this.setPartnerList();
      }
    },

    components: {
      PageMain,
      SearchForm,
      PartnerForm
    }
  }

</script>

<style lang="scss" scoped>
  .partner-list {
    height: 100%;

    .status_0 {
      color: #ff0000;
    }

    .status_1 {
      color: #11b95c;
    }
  }

</style>
